
restore,'./emaf_paper/bgps_emaf.sav',/ver
s   = read_bgps_csv()
d11 = read_mrt('./local/mrt/apj385558t2_mrt.txt')

ind = where(d11.glon GE 10 AND d11.glon LE 65,ns) & print,ns
d11 = d11[ind]
s   = s[ind]


g3 = d11.ego GT 0 OR d11.rms GT 0
g2 = ~g3 AND d11.r08 GT 0
g1 = ~g3 AND ~g2 AND d11.glim GT 0
g0 = ~g3 AND ~g2 AND ~g1

print,fix([total(g3),total(g2),total(g1),total(g0)])

g0i = where(g0)
g1i = where(g1)
g2i = where(g2)
g3i = where(g3)
ei  = where_array(emaf.cnum,s.cnum)

col=['black','cyan','green','red','brown']
bin=0.125

myps,'./emaf_paper/plots/dunham_split.eps',xsize=9

multiplot_xm,[2,1],/xmargin

cgPlot,[0],[0],/nodata,xtit='log!d10!n(S!d1.1!n) [Jy]',$
       ytit='N',xr=[-1.55,1.45],/xst,yr=[0,400],/yst

plothist,alog10(s[g0i].flux),bin=bin,thick=3,/over,color=col[0]
plothist,alog10(s[g1i].flux),bin=bin,thick=3,/over,color=col[1]
plothist,alog10(s[g2i].flux),bin=bin,thick=3,/over,color=col[2]
plothist,alog10(s[g3i].flux),bin=bin,thick=3,/over,color=col[3]
plothist,alog10(s[ei].flux),bin=bin,thick=3,/over,color=col[4]

al_legend,/top,/right,box=0,color=col,linestyle=0,thick=3,$
          ['Group '+string(indgen(4),format="(I0)"),'EMAF']

multiplot

cgPlot,[0],[0],/nodata,xtit='log!d10!n(S!d1.1!n) [Jy]',$
       xr=[-1.55,1.45],/xst,yr=[0,40],/yst
cgAxis,yaxis=1,ytit='N',ytickformat="(I0)"

g3 = g3[ei]
g2 = g2[ei]
g1 = g1[ei]
g0 = g0[ei]
print,fix([total(g3),total(g2),total(g1),total(g0)])

g0i = where(g0)
g1i = where(g1)
g2i = where(g2)
g3i = where(g3)

plothist,alog10(s[g0i].flux),bin=bin,thick=3,/over,color=col[0],x0,y0
plothist,alog10(s[g1i].flux),bin=bin,thick=3,/over,color=col[1],x1,y1
plothist,alog10(s[g2i].flux),bin=bin,thick=3,/over,color=col[2],x2,y2
plothist,alog10(s[g3i].flux),bin=bin,thick=3,/over,color=col[3]

f0 = mpfitpeak(x0,y0,A0,nterms=3)
f1 = mpfitpeak(x1,y1,A1,nterms=3)
f2 = mpfitpeak(x2,y2,A2,nterms=3)

xp = findgen(1001)*(!x.crange[1]-!x.crange[0])/1.d3+!x.crange[0]

cgOplot,xp,gauss_1(xp,A0),color=col[0],thick=5
cgOplot,xp,gauss_1(xp,A1),color=col[1],thick=5
cgOplot,xp,gauss_1(xp,A2),color=col[2],thick=5


al_legend,/top,/right,box=0,color=col[0:3],linestyle=0,thick=3,$
          ['Group '+string(indgen(4),format="(I0)")]

;; multiplot,/reset
;; multiplot,/default
;; multiplot,/reset
myps,/done,/mp

END
